using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class RepositoryDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public RepositoryDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Repository table.
		/// </summary>
		public void Insert(RepositoryDTO repository)
		{
			ValidationUtility.ValidateArgument("repository", repository);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", repository.Id),
				new SqlParameter("@Name", repository.Name),
				new SqlParameter("@Address", repository.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_RepositoryInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Repository table.
		/// </summary>
		public void Update(RepositoryDTO repository)
		{
			ValidationUtility.ValidateArgument("repository", repository);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", repository.Id),
				new SqlParameter("@Name", repository.Name),
				new SqlParameter("@Address", repository.Address)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_RepositoryUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the sp_Repository table by its primary key.
		/// </summary>
		public void Delete(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_RepositoryDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Repository table.
		/// </summary>
		public RepositoryDTO Select(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_RepositorySelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Repository table.
		/// </summary>
		public string SelectJson(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_RepositorySelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Repository table.
		/// </summary>
		public List<RepositoryDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_RepositorySelectAll"))
			{
				List<RepositoryDTO> repositoryDTOList = new List<RepositoryDTO>();
				while (dataReader.Read())
				{
					RepositoryDTO repositoryDTO = MapDataReader(dataReader);
					repositoryDTOList.Add(repositoryDTO);
				}

				return repositoryDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Repository table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_RepositorySelectAll");
		}

		/// <summary>
		/// Creates a new instance of the RepositoryDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private RepositoryDTO MapDataReader(SqlDataReader dataReader)
		{
			RepositoryDTO repositoryDTO = new RepositoryDTO();
			repositoryDTO.Id = dataReader.GetString("Id", String.Empty);
			repositoryDTO.Name = dataReader.GetString("Name", null);
			repositoryDTO.Address = dataReader.GetString("Address", null);

			return repositoryDTO;
		}

		#endregion
	}
}
